#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define mem(x) memset(x,0,sizeof(x))
#define endl "\n"
#define printYes cout << "Yes\n"
#define printYES cout << "YES\n"
#define printNo cout << "No\n"
#define printNO cout << "NO\n"
#define lowbit(x) ((x)&(-(x)))
#define pb push_back
#define mkp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define rep(i,j,k) for (int i=(j);i<=(k);i++)
#define per(i,j,k) for (int i=(j);i>=(k);i--)
#define pcnt(x) __builtin_popcount(x)
mt19937 rnd(time(0));
template<class T>void chkmin(T&x,T y){x=min(x,y);}
template<class T>void chkmax(T&x,T y){x=max(x,y);}

const ll inf=1000000000000000000; 
//const ll inf=1000000000;
//const ll mod=998244353;
//const ll mod=1000000007;

const int N=100005,M=N*20;
int n,m; 
ll dp[N];
struct seg
{
	int tot;
	ll s[M];
	int lc[M],rc[M];
	int rt[N];
	inline int clone(int u)
	{
		++tot;
		lc[tot]=lc[u],rc[tot]=rc[u],s[tot]=s[u];
		return tot;
	}
	inline void push_up(int u)
	{
		s[u]=s[lc[u]]+s[rc[u]];
	}
	int update(int u,int l,int r,int x,int y)
	{
		u=clone(u);
		if (l==r)
		{
			s[u]+=y;
			return u;
		}
		int mid=(l+r)>>1;
		if (x<=mid) lc[u]=update(lc[u],l,mid,x,y);
		else rc[u]=update(rc[u],mid+1,r,x,y);
		push_up(u);
		return u;
	}
	ll query(int u,int L,int R,int l,int r)
	{
		if (!u) return 0; 
		if (l<=L&&R<=r) return s[u];
		int mid=(L+R)>>1;
		ll res=0;
		if (l<=mid) res+=query(lc[u],L,mid,l,r);
		if (mid<r) res+=query(rc[u],mid+1,R,l,r);
		return res;
	}
}a[3];
inline ll Query(int L,int R,int l,int r,int tp)
{
	return a[tp].query(a[tp].rt[r],1,n,L,R)-a[tp].query(a[tp].rt[l-1],1,n,L,R);
}
ll Query(int L,int R,int l,int r,ll x,ll y,ll z)
{
	if (L>R||l>r) return 0;
	ll res=0;
	if (x) res+=Query(L,R,l,r,0)*x;
	if (y) res+=Query(L,R,l,r,1)*y;
	if (z) res+=Query(L,R,l,r,2)*z;
	return res;
}

ll calc(int l,int r)
{
	ll res=0;
	int mid=(l+r)>>1;
	res+=Query(1,l,r,n,0);
	res+=Query(l,n,1,r,r-l,1,-1);
	res+=Query(1,l-1,l,mid,-l,0,1);
	res+=Query(l,mid,r+1,n,-l,1,0);
	res+=Query(1,l-1,mid+1,r,r,0,-1);
	res+=Query(mid+1,r,r+1,n,r,-1,0);
	return res;
}
inline ll val(int l,int r)
{
	return dp[l]+calc(l,r);
}
struct node
{
	int l,r,p;
}q[N];
vector<int>t[N];

int main()
{
	freopen("block.in","r",stdin);
	freopen("block.out","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> m;
	rep(i,1,m)
	{
		int l,r;
		cin >> l >> r;
		t[r].pb(l);
	}
	rep(i,1,n)
	{
		rep(j,0,2) a[j].rt[i]=a[j].rt[i-1];
		for (int j:t[i]) 
		{
			a[0].rt[i]=a[0].update(a[0].rt[i],1,n,j,1);
			a[1].rt[i]=a[1].update(a[1].rt[i],1,n,j,j);
			a[2].rt[i]=a[2].update(a[2].rt[i],1,n,j,i);
		}
	}
	int L=1,R=1;
	q[1]=(node){1,n,1};
	rep(i,2,n)
	{
		while (L<=R&&q[L].r<i) L++;
		q[L].l=i;
		dp[i]=val(q[L].p,i);
		while (L<R&&val(i,q[R].l)<=val(q[R].p,q[R].l)) R--;
		int l=q[R].l,r=q[R].r;
		while (l<=r)
		{
			int mid=(l+r)>>1;
			if (val(i,mid)<=val(q[R].p,mid)) r=mid-1;
			else l=mid+1;
		}
		if (l<=n) q[R].r=l-1,q[++R]=(node){l,n,i};
	}
	cout << dp[n];
	return 0;
}
